#include "head.h"
// 写入数据到文件中
void user_fprint(u_list *list, const char *file_name)
{
    // 创建文件指针
    FILE *fp = fopen(file_name, "w");
    if (NULL == fp)
    {
        ERRLOG("文件创建失败\n");
        return;
    }
    // 光标移动到开头
    fseek(fp, 0, SEEK_SET);
    // 定义指针指向
    u_node *ptr = list->next;
    // 遍历写入
    while (NULL != ptr)
    {
        fprintf(fp, "%s\t%s\t%d\t%d\n", ptr->user.name, ptr->user.password, ptr->user.IDcard, ptr->user.value);
        ptr = ptr->next;
    }
    // 关闭文件指针
    fclose(fp);
}
// 从文件中读取数据
void uesr_fscanf(u_list *list, const char *file_name)
{
    // 创建文件指针
    FILE *fp = fopen(file_name, "r");
    if (NULL == fp)
    {
        ERRLOG("文件创建失败\n");
        return;
    }
    // 光标移动到开头
    fseek(fp, 0, SEEK_SET);
    // 判断文件行数
    char ch = '0'; // 接收文件的字符
    int len = 0;   // 判断文件的行数
    while (EOF != (ch = fgetc(fp)))
    {
        if ('\n' == ch)
        {
            len++;
        }
    }
    // 移动光标到开头
    fseek(fp, 0, SEEK_SET);
    f_user data;
    for (size_t i = 0; i < len; i++)
    {
        fscanf(fp, "%s\t%s\t%d\t%d\n", data.name, data.password, &data.IDcard, &data.value);
        user_insert(list, data, i + 1);
    }
    fclose(fp);
    return;
}

// 登录
f_user log_in(u_list *list)
{
    if (NULL == list)
    {
        ERRLOG("链表的动态空间为NULL\n");
        exit(-1);
    }
    if (NULL == list->next)
    {
        ERRLOG("链表的动态空间无数据\n");
        exit(-1);
    }
    // 登录次数控制
    int num = 3;
    f_user log_user;
    while (1)
    {
        // 用户账号以及密码
        printf("输入你的账号:");
        scanf("%s", log_user.name);
        printf("输入你的密码:");
        scanf("%s", log_user.password);
        u_node *ptr = list->next;
        num--;
        // 登录账号
        if (NULL != (ptr = user_find_name(list, log_user.name)))
        {
            // 登录密码
            if (!strcmp(ptr->user.password, log_user.password))
            {

                printf("登录成功\n");
                log_user = ptr->user;
                return log_user;
            }
            // 输入错误提示
            else
            {
                if (num == 0)
                {
                    num = 3;
                    printf("三次输入错误,请稍后再重试!\n");
                    sleep(5);
                    system("clear");
                }
                else
                {
                    printf("账号或密码输入错误,请重试!还有%d次机会\n\n", num);
                    sleep(3);
                    system("clear");
                }
            }
        }
        // 输入错误提示
        else
        {
            if (num == 0)
            {
                num = 3;
                printf("三次输入错误,请稍后再重试!\n");
                sleep(5);
                system("clear");
            }
            else
            {
                printf("账号或密码输入错误,请重试!还有%d次机会\n\n", num);
                sleep(3);
                system("clear");
            }
        }
    }
}

// 注册
void reg_in(u_list *list)
{
    f_user log_data;
    char password[128];
    // 等级、余额设定
    log_data.IDcard = 1;
    log_data.value = 0;
    while (1)
    {
        printf("请输入你要注册的账号:\n");
        scanf("%s", log_data.name);
        printf("请输入你要注册此账号的密码:\n");
        scanf("%s", log_data.password);
        printf("再次输入你的密码:\n");
        scanf("%s", password);
        if (strcmp(password, log_data.password))
        {
            printf("两次输入的密码不相同 请重新运行本程序\n");
            break;
        }
        printf("注册成功,欢迎使用\n");
        // 插入数据
        user_insert(list, log_data, 1);
        // 写入文件
        user_fprint(list, REGULAR_FILE);
    }
}

// 用户
// 用户--购票退票
void buy_and_return_ticket(FlightSystem *list, f_user *data)
{
    int i = 0;
    char ch[20];                               // 输入字符串读取
    int choice;                                // 航班的选择
    int buy_after;                             // 购买后 余额 变化 记录
    int quit_buy_after;                        // 退票后 余额 变化 记录
    f_node *ptr = list->Flight_list->next;     // 航班
    u_node *ptr_1 = list->REGULAR_USER->next;  // 普通用户
    u_node *ptr_2 = list->SILVER_USER->next;   // 白银用户
    u_node *ptr_3 = list->GOLD_USER->next;     // 黄金用户
    u_node *ptr_4 = list->PLATINUM_USER->next; // 白金用户
    while (1)
    {
        printf("\n购票、退票系统\n");
        printf("请根据需求输入'购票'或'退票'或'退出'\n");
        scanf("%s", ch);
        if (0 == strcmp(ch, "购票"))
        {
            // 遍历双向循环链表,读取数据
            print_forward_double_link_list(list->Flight_list);
            printf("您的余额为:%.2f\n", (float)data->value);
            printf("请选择你需要购买的班次:");
            scanf("%d", &choice);
            do
            {
                if (choice == ptr->data.id)
                {
                    printf("请确认您要购买的班次是:航班班次：%d  机型：%s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票 价: %d\n\n", ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity, ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1], ptr->data.flytime, ptr->data.value);
                    switch (data->IDcard)
                    {
                    case 1:
                        printf("普通会员暂无折扣\n");
                        break;
                    case 2:
                        printf("打完折后票价为: %.2f\n", ptr->data.value * 0.9);
                        break;
                    case 3:
                        printf("打完折后票价为: %.2f\n", ptr->data.value * 0.8);
                        break;
                    case 4:
                        printf("打完折后票价为: %.2f\n", ptr->data.value * 0.7);
                        break;
                    default:
                        break;
                    }
                    printf("请确认无误后, 输入'确认'完成购买此票\n");
                    printf("若有错误，请输入'返回'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "确认"))
                    {
                        switch (data->IDcard)
                        {
                        // 普通用户余额变化
                        case 1:
                            do
                            {
                                if (0 == strcmp(ptr_1->user.name, data->name) && 0 == strcmp(ptr_1->user.password, data->password))
                                {
                                    if (data->value > ptr->data.value)
                                    {
                                        // 购买后 航班剩余座位 变化
                                        ptr->data.leftbuyersa--;
                                        flight_data_file_print(list->Flight_list);
                                        // 购买后 余额  订单 变化
                                        buy_after = data->value - ptr->data.value;
                                        data->value = buy_after;
                                        ptr_1->user.value = data->value;
                                        user_fprint(list->REGULAR_USER, REGULAR_FILE);
                                        // 机票数据写入顺序表
                                        arr_insert(data->ticket, ptr->data);
                                        printf("请稍等,正在为您购买·····\n");
                                        sleep(3);
                                        printf("购票成功!\n");
                                        printf("您的余额还剩:%.2f\n", (float)data->value);
                                        sleep(1);
                                        printf("请在我的订单内查看相关信息\n");
                                        check_my_ticket(data);
                                        system("clear");
                                        break;
                                    }
                                    else if (data->value < ptr->data.value)
                                    {
                                        printf("余额不足,请充值后进行购买\n");
                                        sleep(2);
                                        system("clear");
                                        break;
                                    }
                                }
                                ptr_1 = ptr_1->next;
                            } while (list->REGULAR_USER->next != NULL);
                            break;
                        // 白银用户余额变化
                        case 2:
                            do
                            {
                                if (0 == strcmp(ptr_1->user.name, data->name) && 0 == strcmp(ptr_1->user.password, data->password))
                                {
                                    if (data->value > (ptr->data.value * 0.9))
                                    {
                                        // 购买后 航班剩余座位 变化
                                        ptr->data.leftbuyersa--;
                                        flight_data_file_print(list->Flight_list);
                                        // 购买后 余额  订单 变化
                                        buy_after = data->value - (ptr->data.value * 0.9);
                                        data->value = buy_after;
                                        ptr_2->user.value = data->value;
                                        user_fprint(list->SILVER_USER, SILVER_FILE);
                                        // 机票数据写入顺序表
                                        arr_insert(data->ticket, ptr->data);
                                        printf("请稍等,正在为您购买·····\n");
                                        sleep(3);
                                        printf("购票成功!\n");
                                        printf("您的余额还剩:%.2f\n", (float)data->value);
                                        sleep(1);
                                        printf("请在我的订单内查看相关信息\n");
                                        check_my_ticket(data);
                                        system("clear");
                                        break;
                                    }
                                    else if (data->value < (ptr->data.value * 0.9))
                                    {
                                        printf("余额不足,请充值后进行购买\n");
                                        break;
                                    }
                                }
                                ptr_2 = ptr_2->next;
                            } while (list->SILVER_USER->next != NULL);
                            break;
                        // 黄金用户余额变化
                        case 3:
                            do
                            {
                                if (0 == strcmp(ptr_1->user.name, data->name) && 0 == strcmp(ptr_1->user.password, data->password))
                                {
                                    if (data->value > (ptr->data.value * 0.8))
                                    {
                                        // 购买后 航班剩余座位 变化
                                        ptr->data.leftbuyersa--;
                                        flight_data_file_print(list->Flight_list);
                                        // 购买后 余额  订单 变化
                                        buy_after = data->value - (ptr->data.value * 0.8);
                                        data->value = buy_after;
                                        ptr_3->user.value = data->value;
                                        user_fprint(list->GOLD_USER, GOLD_FILE);
                                        // 机票数据写入顺序表
                                        arr_insert(data->ticket, ptr->data);
                                        printf("请稍等,正在为您购买·····\n");
                                        sleep(3);
                                        printf("购票成功!\n");
                                        printf("您的余额还剩:%.2f\n", (float)data->value);
                                        sleep(1);
                                        printf("请在我的订单内查看相关信息\n");
                                        check_my_ticket(data);
                                        system("clear");
                                        break;
                                    }
                                    else if (data->value < (ptr->data.value * 0.8))
                                    {
                                        printf("余额不足,请充值后进行购买\n");
                                        break;
                                    }
                                }
                                ptr_3 = ptr_3->next;
                            } while (list->GOLD_USER->next != NULL);
                            break;
                        // 白金用户余额变化
                        case 4:
                            do
                            {
                                if (0 == strcmp(ptr_1->user.name, data->name) && 0 == strcmp(ptr_1->user.password, data->password))
                                {
                                    if (data->value > (ptr->data.value * 0.7))
                                    {
                                        // 购买后 航班剩余座位 变化
                                        ptr->data.leftbuyersa--;
                                        flight_data_file_print(list->Flight_list);
                                        // 购买后 余额  订单 变化
                                        buy_after = data->value - (ptr->data.value * 0.7);
                                        data->value = buy_after;
                                        ptr_4->user.value = data->value;
                                        user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                                        // 机票数据写入顺序表
                                        arr_insert(data->ticket, ptr->data);
                                        printf("请稍等,正在为您购买·····\n");
                                        sleep(3);
                                        printf("购票成功!\n");
                                        printf("您的余额还剩:%.2f\n", (float)data->value);
                                        sleep(1);
                                        printf("请在我的订单内查看相关信息\n");
                                        check_my_ticket(data);
                                        system("clear");
                                        break;
                                    }
                                    else if (data->value < (ptr->data.value * 0.7))
                                    {
                                        printf("余额不足,请充值后进行购买\n");
                                        break;
                                    }
                                }
                                ptr_4 = ptr_4->next;
                            } while (list->PLATINUM_USER->next != NULL);
                            break;
                        default:
                            break;
                        }
                        break;
                    }
                    else if (0 == strcmp(ch, "返回"))
                    {
                        break;
                    }
                }
                ptr = ptr->next;
            } while (list->Flight_list->next != ptr);
        }
        else if (0 == strcmp(ch, "退票"))
        {
            check_my_ticket(data);
            if (data->ticket->len == 0)
            {
                break;
            }
            printf("请选择想要取消的航班的'班次': ");
            scanf("%d", &choice);
            i = arr_find_by_name(data->ticket, choice);
            if (EOF != i)
            {
                printf("请确认您要取消的航班是:航班班次：%d  机型：%s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票 价: %d\n\n", data->ticket->data[i].id, data->ticket->data[i].flighttype, data->ticket->data[i].startcity, data->ticket->data[i].arrivecity, data->ticket->data[i].starttime[0], data->ticket->data[i].starttime[1], data->ticket->data[i].arrivetime[0], data->ticket->data[i].arrivetime[1], data->ticket->data[i].flytime, data->ticket->data[i].value);
                printf("请输入'确认'完成取消该航班\n");
                printf("若有错误，请输入'返回'\n");
                scanf("%s", ch);
                if (0 == strcmp(ch, "确认"))
                {
                    // 退票后 航班座位 变化
                    ptr->data.leftbuyersa++;
                    flight_data_file_print(list->Flight_list);
                    switch (data->IDcard)
                    {
                    // 普通用户余额变化
                    case 1:
                        do
                        {
                            if (0 == strcmp(ptr_1->user.name, data->name) && 0 == strcmp(ptr_1->user.password, data->password))
                            {
                                // 退票后 用户余额 变化
                                quit_buy_after = data->value + data->ticket->data[i].value;
                                data->value = quit_buy_after;
                                ptr_1->user.value = quit_buy_after;
                                user_fprint(list->REGULAR_USER, REGULAR_FILE);
                                break;
                            }
                            ptr_1 = ptr_1->next;
                        } while (list->REGULAR_USER->next != NULL);
                        break;
                    // 白银用户余额变化
                    case 2:
                        do
                        {
                            if (0 == strcmp(ptr_2->user.name, data->name) && 0 == strcmp(ptr_2->user.password, data->password))
                            {
                                // 退票后 用户余额 变化
                                quit_buy_after = data->value + (data->ticket->data[i].value * 0.9);
                                data->value = quit_buy_after;
                                ptr_2->user.value = quit_buy_after;
                                user_fprint(list->SILVER_USER, SILVER_FILE);
                                break;
                            }
                            ptr_2 = ptr_2->next;
                        } while (list->SILVER_USER->next != NULL);
                        break;
                    // 黄金用户余额变化
                    case 3:
                        do
                        {
                            if (0 == strcmp(ptr_3->user.name, data->name) && 0 == strcmp(ptr_3->user.password, data->password))
                            {
                                // 退票后 用户余额 变化
                                quit_buy_after = data->value + (data->ticket->data[i].value * 0.8);
                                data->value = quit_buy_after;
                                ptr_3->user.value = quit_buy_after;
                                user_fprint(list->GOLD_USER, GOLD_FILE);
                                break;
                            }
                            ptr_3 = ptr_3->next;
                        } while (list->GOLD_USER->next != NULL);
                        break;
                    // 白金用户余额变化
                    case 4:
                        do
                        {
                            if (0 == strcmp(ptr_4->user.name, data->name) && 0 == strcmp(ptr_4->user.password, data->password))
                            {
                                // 退票后 用户余额 变化
                                quit_buy_after = data->value + (data->ticket->data[i].value * 0.7);
                                data->value = quit_buy_after;
                                ptr_4->user.value = quit_buy_after;
                                user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                                break;
                            }
                            ptr_4 = ptr_4->next;
                        } while (list->PLATINUM_USER->next != NULL);
                        break;
                    default:
                        break;
                    }
                    pos_del_list(data->ticket, choice);
                    printf("请稍后,正在为您退票·····\n");
                    sleep(3);
                    printf("退票成功!\n");
                    printf("余额已返还,请及时查看\n\n");
                    printf("欢迎再次乘坐该航班!\n");
                    check_my_ticket(data);
                }
                else if (0 == strcmp(ch, "返回"))
                {
                    break;
                }
            }
            else if (EOF == i)
            {
                printf("没有该航班\n");
            }
            i++;
        }
        else if (0 == strcmp(ch, "退出"))
        {
            break;
        }
        else
            printf("输入错误,请重新输入\n\n");
    }
}

// 用户-- 查看订单
void check_my_ticket(f_user *L)
{
    if (L->ticket->len == 0)
    {
        printf("您暂无订单!\n\n");
        printf("按回车返回\n");
    }
    else
    {
        printf("您的订单如下:\n");
        printf_Sql_List(L->ticket);
        printf("按回车继续\n");
    }
    // 缓存
    getchar();
    getchar();
}

// 用户--修改信息 -- 账号和密码
void user_change_password(FlightSystem *list, f_user *data)
{
    char sh;
    char ch[20];
    char buf[128];                             // 记录密码
    char buf_1[128];                           // 记录密码
    u_node *ptr_1 = list->REGULAR_USER->next;  // 普通用户
    u_node *ptr_2 = list->SILVER_USER->next;   // 白银用户
    u_node *ptr_3 = list->GOLD_USER->next;     // 黄金用户
    u_node *ptr_4 = list->PLATINUM_USER->next; // 白金用户
    while (1)
    {
        printf("修改信息系统\n");
        printf("请选择输入修改你的'账号'或'密码'或'退出'\n");
        scanf("%s", ch);
        if (0 == strcmp(ch, "账号"))
        {
            printf("请输入你的旧账号:\n");
            scanf("%s", ch);
            if (0 == strcmp(data->name, ch))
            {
                printf("请输入你的新帐号:\n");
                scanf("%s", ch);
                switch (data->IDcard)
                {
                // 普通用户
                case 1:
                    // 遍历寻找
                    do
                    {
                        if (0 == strcmp(ptr_1->user.name, data->name))
                        {
                            strcpy(ptr_1->user.name, ch);
                            strcpy(data->name, ptr_1->user.name);
                            user_fprint(list->REGULAR_USER, REGULAR_FILE);
                            break;
                        }
                        ptr_1 = ptr_1->next;
                    } while (list->REGULAR_USER->next != NULL);
                    break;
                // 白银用户
                case 2:
                    do
                    {
                        if (0 == strcmp(ptr_2->user.name, data->name))
                        {
                            strcpy(ptr_2->user.name, ch);
                            strcpy(data->name, ptr_2->user.name);
                            user_fprint(list->SILVER_USER, SILVER_FILE);
                            break;
                        }
                        ptr_2 = ptr_2->next;
                    } while (list->SILVER_USER->next != NULL);
                    break;
                // 黄金用户
                case 3:
                    do
                    {
                        if (0 == strcmp(ptr_3->user.name, data->name))
                        {
                            strcpy(ptr_3->user.name, ch);
                            strcpy(data->name, ptr_3->user.name);
                            user_fprint(list->GOLD_USER, GOLD_FILE);
                            break;
                        }
                        ptr_3 = ptr_3->next;
                    } while (list->GOLD_USER->next != NULL);
                    break;
                // 白金用户
                case 4:
                    do
                    {
                        if (0 == strcmp(ptr_4->user.name, data->name))
                        {
                            strcpy(ptr_4->user.name, ch);
                            strcpy(data->name, ptr_4->user.name);
                            user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                            break;
                        }
                        ptr_4 = ptr_4->next;
                    } while (list->PLATINUM_USER->next != NULL);
                    break;
                default:
                    break;
                }
                printf("请稍候,正在为你修改·····\n");
                sleep(3);
                printf("修改账号成功\n");
                printf("\n按回车继续\n");
                getchar();
                scanf("%c", &sh);
                if (sh == 10)
                    system("clear");
                break;
            }
            // 错误提示
            else if (strcmp(data->name, ch))
            {
                printf("您输入的账号与您的账号不匹配\n");
                printf("请稍等重新输入\n");
                sleep(3);
                system("clear");
            }
        }
        else if (0 == strcmp(ch, "密码"))
        {
            printf("请输入你的旧密码:\n");
            scanf("%s", ch);
            if (0 == strcmp(data->password, ch))
            {
                printf("请输入你的新密码:\n");
                scanf("%s", buf);
                if (0 == strcmp(ch, buf))
                {
                    printf("新旧两次密码不能相同,请重试\n");
                    printf("请稍等重新输入\n");
                    sleep(3);
                    system("clear");
                }
                else if (strcmp(ch, buf))
                {
                    printf("请再次输入相同的新密码:\n");
                    scanf("%s", buf_1);
                    if (0 == strcmp(buf, buf_1))
                    {
                        switch (data->IDcard)
                        {
                        // 普通用户
                        case 1:
                            do
                            { // 双重判断,以防数据写入到同类型其他用户内
                                if (0 == strcmp(ptr_1->user.password, data->password) && 0 == strcmp(data->name, ptr_1->user.name))
                                {
                                    strcpy(ptr_1->user.password, buf_1);
                                    strcpy(data->password, ptr_1->user.password);
                                    user_fprint(list->REGULAR_USER, REGULAR_FILE);
                                    break;
                                }
                                ptr_1 = ptr_1->next;
                            } while (list->REGULAR_USER->next != NULL);
                            break;
                        // 白银用户
                        case 2:
                            do
                            {
                                if (0 == strcmp(ptr_2->user.password, data->password) && 0 == strcmp(data->name, ptr_2->user.name))
                                {
                                    strcpy(ptr_2->user.password, buf_1);
                                    strcpy(data->password, ptr_2->user.password);
                                    user_fprint(list->SILVER_USER, SILVER_FILE);
                                    break;
                                }
                                ptr_2 = ptr_2->next;
                            } while (list->SILVER_USER->next != NULL);
                            break;
                        // 黄金用户
                        case 3:
                            do
                            {
                                if (0 == strcmp(ptr_3->user.password, data->password) && 0 == strcmp(data->name, ptr_3->user.name))
                                {
                                    strcpy(ptr_3->user.password, buf_1);
                                    strcpy(data->password, ptr_3->user.password);
                                    user_fprint(list->GOLD_USER, GOLD_FILE);
                                    break;
                                }
                                ptr_3 = ptr_3->next;
                            } while (list->GOLD_USER->next != NULL);
                            break;
                        // 白金用户
                        case 4:
                            do
                            {
                                if (0 == strcmp(ptr_4->user.password, data->password) && 0 == strcmp(data->name, ptr_4->user.name))
                                {
                                    strcpy(ptr_4->user.password, buf_1);
                                    strcpy(data->password, ptr_4->user.password);
                                    user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                                    break;
                                }
                                ptr_4 = ptr_4->next;
                            } while (list->PLATINUM_USER->next != NULL);
                            break;
                        default:
                            break;
                        }
                        printf("请稍候,正在为你修改·····\n");
                        sleep(3);
                        printf("修改账号成功\n");
                        printf("\n按回车继续\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    // 错误提示
                    else if (strcmp(buf, buf_1))
                    {
                        printf("两次输入密码不相同,请重试\n");
                        printf("请稍等重新输入\n");
                        sleep(3);
                        system("clear");
                    }
                }
            }
            // 错误提示
            else if (strcmp(data->password, ch))
            {
                printf("您输入的密码与您的原密码不匹配\n");
                printf("请稍等重新输入\n");
                sleep(3);
                system("clear");
            }
        }
        else if (0 == strcmp(ch, "退出"))
        {
            break;
        }
        // 错误提示
        else
        {
            printf("输入错误,请重新输入\n");
            sleep(2);
            system("clear");
        }
    }
}

// 用户--充值余额
void user_get_value(FlightSystem *list, f_user *data)
{

    char ch[20];
    int money;
    int sum;
    char sh;
    u_node *ptr_1 = list->REGULAR_USER->next;  // 普通用户
    u_node *ptr_2 = list->SILVER_USER->next;   // 白银用户
    u_node *ptr_3 = list->GOLD_USER->next;     // 黄金用户
    u_node *ptr_4 = list->PLATINUM_USER->next; // 白金用户
    while (1)
    {
        system("clear");
        printf("\n充值系统\n");
        printf("您现在的余额为:%d\n", data->value);
        printf("\n请选择'充值'或'退出'\n");
        scanf("%s", ch);
        if (0 == strcmp(ch, "充值"))
        {
            printf("\n温馨提示:");
            printf("一次性充值的余额不宜过多,请一次充值不要超过1w\n");
            printf("请输入你想要充值的余额:\n");
            scanf("%d", &money);
            getchar();
            if (0 < money && money < 10000)
            {
                printf("\n请输入'确认'是否充值 %d\n", money);
                printf("如有错误,请输入'返回'\n\n");
                scanf("%s", ch);
                while (1)
                {
                    if (0 == strcmp(ch, "确认"))
                    {
                        switch (data->IDcard)
                        {
                        // 普通用户
                        case 1:
                            do
                            {
                                if (ptr_1->user.value == data->value)
                                {
                                    sum = money + ptr_1->user.value;
                                    ptr_1->user.value = sum;
                                    data->value = ptr_1->user.value;
                                    user_fprint(list->REGULAR_USER, REGULAR_FILE);
                                    break;
                                }
                                ptr_1 = ptr_1->next;
                            } while (list->REGULAR_USER->next != NULL);
                            break;
                        // 白银用户
                        case 2:
                            do
                            {
                                if (ptr_2->user.value == data->value)
                                {
                                    sum = money + ptr_2->user.value;
                                    ptr_2->user.value = sum;
                                    data->value = ptr_2->user.value;
                                    user_fprint(list->SILVER_USER, SILVER_FILE);
                                    break;
                                }
                                ptr_2 = ptr_2->next;
                            } while (list->SILVER_USER->next != NULL);
                            break;
                        // 黄金用户
                        case 3:
                            do
                            {
                                if (ptr_3->user.value == data->value)
                                {
                                    sum = money + ptr_3->user.value;
                                    ptr_3->user.value = sum;
                                    data->value = ptr_3->user.value;
                                    user_fprint(list->GOLD_USER, GOLD_FILE);
                                    break;
                                }
                                ptr_3 = ptr_3->next;
                            } while (list->GOLD_USER->next != NULL);
                            break;
                        // 白金用户
                        case 4:
                            do
                            {
                                if (ptr_4->user.value == data->value)
                                {
                                    sum = money + ptr_4->user.value;
                                    ptr_4->user.value = sum;
                                    data->value = ptr_4->user.value;
                                    user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                                    break;
                                }
                                ptr_4 = ptr_4->next;
                            } while (list->PLATINUM_USER->next != NULL);
                            break;
                        default:
                            break;
                        }
                        printf("\n请等待余额到账·····\n");
                        sleep(3);
                        printf("您现在的账户余额为:%d\n", sum);
                        printf("\n按回车继续\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (0 == strcmp(ch, "返回"))
                    {
                        break;
                    }
                    // 错误提示
                    else
                    {
                        printf("输入错误,请重新输入\n");
                        sleep(3);
                        break;
                    }
                }
            }
            // 错误提示
            else
            {
                printf("输入错误,请稍等重新输入\n");
                sleep(3);
                system("clear");
            }
        }
        else if (0 == strcmp(ch, "退出"))
        {
            break;
        }
        else
            printf("输入错误,请重新输入\n");
    }
}

// 用户--查看航班
void check_flight_find(f_list *list)
{
    char ch_1[20];
    char ch_2[20];
    char sh;
    f_node *ptr = list->next;
    while (1)
    {
        print_forward_double_link_list(list);
        // time_t T_1;
        // T_1 = time(NULL);
        // struct tm *l_time = localtime(&T_1);
        // sleep(1);
        // printf("当前时间:%d年%d月%d日", l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday);
        // printf("%d时%d分%d秒\n", l_time->tm_hour, l_time->tm_min, l_time->tm_sec);
        printf("是否进行搜索?\n");
        printf("如需搜索,请输入'是'\n");
        printf("如需退出,请输入'退出'\n");
        scanf("%s", ch_1);
        if (0 == strcmp(ch_1, "是"))
        {
            printf("请输入需要查找的出发地和目的地,为您搜索相关航班\n");
            printf("出发地:");
            scanf("%s", ch_1);
            printf("目的地:");
            scanf("%s", ch_2);
            do
            { // 双重判断地点
                if (0 == strcmp(ch_1, ptr->data.startcity) && 0 == strcmp(ch_2, ptr->data.arrivecity))
                {
                    printf("搜索到相关的航班有:航班班次：%d  机型：%s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票价: %d  载客量: %d  剩余座位: %d  是否已起飞: %c  \n\n", ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity, ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1], ptr->data.flytime, ptr->data.value, ptr->data.maxNUM, ptr->data.leftbuyersa, ptr->data.whetherfly);
                    printf("\n按回车继续\n");
                    getchar();
                    scanf("%c", &sh);
                    if (sh == 10)
                        system("clear");
                    break;
                }
                ptr = ptr->next;
            } while (list->next != ptr);
            if (strcmp(ch_1, ptr->data.startcity) && strcmp(ch_2, ptr->data.arrivecity))
            {
                printf("抱歉,现无此航班班次\n");
                printf("请稍后再试\n");
                sleep(3);
                system("clear");
            }
        }
        else if (0 == strcmp(ch_1, "退出"))
        {
            break;
        }
        // 错误提示
        else
        {
            printf("输入错误,请稍后重新输入\n");
            sleep(3);
            system("clear");
        }
    }
}

// 管理员
// 管理员--设定航班
void root_set_flight(f_list *list)
{
    char ch[20];
    f_data data;
    while (1)
    {
        print_forward_double_link_list(list);
        printf("是否进行设定航班?\n");
        printf("如需设定,请输入'是'\n");
        printf("如需退出,请输入'退出'\n");
        scanf("%s", ch);
        if (0 == strcmp(ch, "是"))
        {
            printf("航班班次:");
            scanf("%d", &data.id);
            printf("飞机机型:");
            scanf("%s", data.flighttype);
            printf("航班起点站:");
            scanf("%s", data.startcity);
            printf("航班终点站:");
            scanf("%s", data.arrivecity);
            printf("起飞时间 时:分:");
            scanf("%d:%d", &data.starttime[0], &data.starttime[1]);
            printf("到达时间 时:分:");
            scanf("%d:%d", &data.arrivetime[0], &data.arrivetime[1]);
            printf("飞行时间:");
            scanf("%s", data.flytime);
            printf("票价:");
            scanf("%d", &data.value);
            printf("额定载客量:");
            scanf("%d", &data.maxNUM);
            printf("剩余座位数量:");
            scanf("%d", &data.leftbuyersa);
            printf("是否起飞:");
            getchar();
            scanf("%c", &data.whetherfly);
            printf("设定成功!\n\n");
            insert_double_node(list, data, data.id);
            flight_data_file_print(list);
            print_forward_double_link_list(list);
        }
        else if (0 == strcmp(ch, "退出"))
        {
            break;
        }
    }
}
// 管理员--修改航班- 1、改票价 2、改时间 3、删除
void root_update_flight(f_list *list)
{
    int time_1[2];   // 起飞时间读取
    int time_2[2];   // 到达时间读取
    char time_3[20]; // 飞行时间读取
    int money;       // 票价修改
    int choice;      // 航班班次选择
    char ch[20];     // 字符串读取
    char sh;
    f_node *ptr = list->next;
    while (1)
    {
        print_forward_double_link_list(list);
        printf("如若修改,请输入'修改'\n");
        printf("如若删除,请输入'删除'\n");
        printf("如若退出,请输入'退出'\n");
        scanf("%s", ch);
        if (0 == strcmp(ch, "修改"))
        {
            printf("请选择你要修改的航班: ");
            scanf("%d", &choice);
            do
            {
                if (choice == ptr->data.id)
                {
                    printf("你要修改的航班班次：%d  机型：%s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票价: %d  载客量: %d  剩余座位: %d  是否已起飞: %c  \n\n", ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity, ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1], ptr->data.flytime, ptr->data.value, ptr->data.maxNUM, ptr->data.leftbuyersa, ptr->data.whetherfly);
                    printf("请根据需求来输入修改'时间'或'票价'或'返回'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "时间"))
                    {
                        printf("起飞时间: ");
                        scanf("%d:%d", &time_1[0], &time_1[1]);
                        ptr->data.starttime[0] = time_1[0];
                        ptr->data.starttime[1] = time_1[1];
                        printf("到达时间: ");
                        scanf("%d:%d", &time_2[0], &time_2[1]);
                        ptr->data.arrivetime[0] = time_2[0];
                        ptr->data.arrivetime[1] = time_2[1];
                        printf("飞行时间: ");
                        scanf("%s", time_3);
                        strcpy(ptr->data.flytime, time_3);
                        flight_data_file_print(list);
                        printf("修改成功\n");
                        printf("请查看是否正确\n");
                        printf("你已修改的航班班次：%d  机型：%s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票价: %d  载客量: %d  剩余座位: %d  是否已起飞: %c  \n\n", ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity, ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1], ptr->data.flytime, ptr->data.value, ptr->data.maxNUM, ptr->data.leftbuyersa, ptr->data.whetherfly);
                        printf("按回车继续\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            break;
                    }
                    else if (0 == strcmp(ch, "票价"))
                    {
                        printf("票价: ");
                        scanf("%d", &money);
                        ptr->data.value = money;
                        flight_data_file_print(list);
                        printf("你已修改的航班班次: %d  机型: %s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票价: %d  载客量: %d  剩余座位: %d  是否已起飞: %c  \n\n", ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity, ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1], ptr->data.flytime, ptr->data.value, ptr->data.maxNUM, ptr->data.leftbuyersa, ptr->data.whetherfly);
                        printf("按回车继续\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            break;
                    }
                    else if (0 == strcmp(ch, "返回"))
                    {
                        break;
                    }
                    else
                    {
                        printf("输入错误,请重新输入\n");
                    }
                }
                ptr = ptr->next;
            } while (ptr != list->next);
            ptr = list->next;
        }
        else if (0 == strcmp(ch, "删除"))
        {
            printf("请选择你要删除的航班: ");
            scanf("%d", &choice);
            do
            {
                if (choice == ptr->data.id)
                {
                    printf("你要删除的航班班次：%d  机型：%s  起点站: %s  终点站: %s  起飞时间: %02d:%02d  到达时间: %02d:%02d  飞行时间: %s  票价: %d  载客量: %d  剩余座位: %d  是否已起飞: %c  \n\n", ptr->data.id, ptr->data.flighttype, ptr->data.startcity, ptr->data.arrivecity, ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1], ptr->data.flytime, ptr->data.value, ptr->data.maxNUM, ptr->data.leftbuyersa, ptr->data.whetherfly);
                    printf("请输入'确认'或'返回'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "确认"))
                    {
                        delete_double_node(list, choice);
                        flight_data_file_print(list);
                        printf("正在删除······\n");
                        sleep(1);
                        printf("删除成功\n");
                        printf("\n按回车继续\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (0 == strcmp(ch, "返回"))
                    {
                        break;
                    }
                }
                ptr = ptr->next;
            } while (list->next != ptr);
            ptr = list->next;
        }
        else if (0 == strcmp(ch, "退出"))
        {
            break;
        }
        else
            printf("输入错误,请重试\n");
    }
}
// 管理员--修改普通用户-1、现金支付 2、重置密码
void root_update_user(FlightSystem *list)
{
    srand(time(NULL));
    int Rand = rand() % 2 + 1; // 随机生成用户的选择
    char sh;
    char ch[20];
    char buf[20];
    int money; // 余额
    int sum;
    u_node *ptr_1 = list->REGULAR_USER->next;  // 普通用户
    u_node *ptr_2 = list->SILVER_USER->next;   // 白银用户
    u_node *ptr_3 = list->GOLD_USER->next;     // 黄金用户
    u_node *ptr_4 = list->PLATINUM_USER->next; // 白金用户
    printf("如若修改,请输入'修改'\n");
    printf("如若退出,请输入'退出'\n");
    scanf("%s", ch);
    if (0 == strcmp(ch, "修改"))
    {
        printf("请根据用户在现场的选择来修改用户的信息\n");
        printf("用户选择1时则进行'余额充值'\n");
        printf("用户选择2时则进行'密码重置'\n");
        printf("%d", Rand);
        sleep(1);
        if (Rand == 1)
        {
            printf("余额充值\n");
            printf("输入要充值的账号:\n");
            scanf("%s", buf);
            do
            {
                // 白金
                if (0 == strcmp(ptr_4->user.name, buf))
                {
                    printf("线下支付一次不能超过5000\n");
                    printf("请输入要充值的额度: ");
                    scanf("%d", &money);
                    if (money < 5000 && money > 0)
                    {
                        sum = ptr_4->user.value + money;
                        ptr_4->user.value = sum;
                        user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                        printf("该用户的余额为:%d\n", ptr_4->user.value);
                        printf("充值成功!\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (money > 5000)
                    {
                        printf("一次支付额度太大,请在5000以内支付充值\n");
                    }
                    else
                        printf("输入错误,请重新输入\n");
                }
                ptr_4 = ptr_4->next;

                // 黄金
                if (0 == strcmp(ptr_3->user.name, buf))
                {
                    printf("线下支付一次不能超过5000\n");
                    printf("请输入要充值的额度: ");
                    scanf("%d", &money);
                    if (money < 5000 && money > 0)
                    {
                        sum = ptr_3->user.value + money;
                        ptr_3->user.value = sum;
                        user_fprint(list->GOLD_USER, GOLD_FILE);
                        printf("该用户的余额为:%d\n", ptr_3->user.value);
                        printf("充值成功!\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (money > 5000)
                    {
                        printf("一次支付额度太大,请在5000以内支付充值\n");
                    }
                    else
                        printf("输入错误,请重新输入\n");
                }
                ptr_3 = ptr_3->next;

                // 白银
                if (0 == strcmp(ptr_2->user.name, buf))
                {
                    printf("线下支付一次不能超过5000\n");
                    printf("请输入要充值的额度: ");
                    scanf("%d", &money);
                    if (money < 5000 && money > 0)
                    {
                        sum = ptr_2->user.value + money;
                        ptr_2->user.value = sum;
                        user_fprint(list->SILVER_USER, SILVER_FILE);
                        printf("该用户的余额为:%d\n", ptr_2->user.value);
                        printf("充值成功!\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (money > 5000)
                    {
                        printf("一次支付额度太大,请在5000以内支付充值\n");
                    }
                    else
                        printf("输入错误,请重新输入\n");
                }
                ptr_2 = ptr_2->next;

                // 普通
                if (0 == strcmp(ptr_1->user.name, buf))
                {
                    printf("线下支付一次不能超过5000\n");
                    printf("请输入要充值的额度: ");
                    scanf("%d", &money);
                    if (money < 5000 && money > 0)
                    {
                        sum = ptr_1->user.value + money;
                        ptr_1->user.value = sum;
                        user_fprint(list->REGULAR_USER, REGULAR_FILE);
                        printf("该用户的余额为:%d\n", ptr_1->user.value);
                        printf("充值成功!\n");
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (money > 5000)
                    {
                        printf("一次支付额度太大,请在5000以内支付充值\n");
                    }
                    else
                        printf("输入错误,请重新输入\n");
                }
                ptr_1 = ptr_1->next;
            } while (NULL != ptr_4 || NULL != ptr_3 || NULL != ptr_2 || NULL != ptr_1);
        }
        else if (Rand == 2)
        {
            printf("密码重置\n");
            printf("请输入该用户的账号: ");
            scanf("%s", buf);
            do
            {
                // 白金
                if (0 == strcmp(ptr_4->user.name, buf))
                {
                    printf("找到该用户,是否进行密码重置?\n");
                    printf("请输入'是'或'否'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "是"))
                    {
                        strcpy(ptr_4->user.password, "123");
                        printf("重置成功\n");
                        printf("重置密码为123,请用户尽快修改密码\n");
                        user_fprint(list->PLATINUM_USER, PLATINUM_FILE);
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (0 == strcmp(ch, "否"))
                    {
                        return;
                    }
                    else
                        printf("输入错误,请重试\n");
                }
                ptr_4 = ptr_4->next;

                // 黄金
                if (0 == strcmp(ptr_3->user.name, buf))
                {
                    printf("找到该用户,是否进行密码重置?\n");
                    printf("请输入'是'或'否'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "是"))
                    {
                        strcpy(ptr_3->user.password, "123");
                        printf("重置成功\n");
                        printf("重置密码为123,请用户尽快修改密码\n");
                        user_fprint(list->GOLD_USER, GOLD_FILE);
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (0 == strcmp(ch, "否"))
                    {
                        return;
                    }
                    else
                        printf("输入错误,请重试\n");
                }
                ptr_3 = ptr_3->next;

                // 白银
                if (0 == strcmp(ptr_2->user.name, buf))
                {
                    printf("找到该用户,是否进行密码重置?\n");
                    printf("请输入'是'或'否'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "是"))
                    {
                        strcpy(ptr_2->user.password, "123");
                        printf("重置成功\n");
                        printf("重置密码为123,请用户尽快修改密码\n");
                        user_fprint(list->SILVER_USER, SILVER_FILE);
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (0 == strcmp(ch, "否"))
                    {
                        return;
                    }
                    else
                        printf("输入错误,请重试\n");
                }
                ptr_2 = ptr_2->next;

                // 普通
                if (0 == strcmp(ptr_1->user.name, buf))
                {
                    printf("找到该用户,是否进行密码重置?\n");
                    printf("请输入'是'或'否'\n");
                    scanf("%s", ch);
                    if (0 == strcmp(ch, "是"))
                    {
                        strcpy(ptr_1->user.password, "123");
                        printf("重置成功\n");
                        printf("重置密码为123,请用户尽快修改密码\n");
                        user_fprint(list->REGULAR_USER, REGULAR_FILE);
                        getchar();
                        scanf("%c", &sh);
                        if (sh == 10)
                            system("clear");
                        break;
                    }
                    else if (0 == strcmp(ch, "否"))
                    {
                        return;
                    }
                    else
                        printf("输入错误,请重试\n");
                }
                ptr_1 = ptr_1->next;
            } while (NULL != ptr_4 || NULL != ptr_3 || NULL != ptr_2 || NULL != ptr_1);
        }
    }
    else if (0 == strcmp(ch, "退出"))
    {
        return;
    }
    else
        printf("输入错误,请重试\n");
}
// 管理员--增加超级用户
void root_add_root(FlightSystem *list)
{

    f_user data;
    char sh;
    char ch[20];
    char password[30];
    printf("如若添加管理员,请输入'添加'\n");
    printf("如若退出,请输入'退出'\n");
    scanf("%s", ch);
    if (0 == strcmp(ch, "添加"))
    {
        data.IDcard = 0;
        data.value = 0;
        printf("请输入要添加管理员的账号: ");
        scanf("%s", data.name);
        printf("请输入要添加管理员的密码: ");
        scanf("%s", password);
        printf("请输入再次输入要添加管理员的密码: ");
        scanf("%s", data.password);
        if (0 == strcmp(password, data.password))
        {
            printf("添加成功!\n");
            getchar();
            scanf("%c", &sh);
            if (sh == 10)
                system("clear");
        }
        else if (strcmp(password, data.password))
        {
            printf("两次输入的密码不同,请重新输入\n");
        }
        else
            printf("输入错误,请重新输入\n");
    }
    else if (0 == strcmp(ch, "退出"))
    {
        return;
    }
    else
        printf("输入错误,请重新输入\n");
    user_insert(list->ADMIN_USER, data, 1);
    user_fprint(list->ADMIN_USER, ADMIN_FILE);
}
// 退出登录
void log_quit()
{
    exit(-1);
}
